package com.egg.demo.rabbitmq.sender;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.egg.common.core.constant.rabbit.RabbitRoutingKey;
import com.egg.common.util.IdGenUtil;
import com.egg.demo.constants.DemoRabbitExchange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 发送
 *
 * @author east
 */
@Slf4j
@Component
public class DemoSender {


    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送-示例
     * 本系统渠道 + 公共KEY
     *
     * @param json
     */
    public void send(JSONObject json) {
        if (ObjectUtil.isEmpty(json)) {
            return;
        }
        String messageId = IdGenUtil.strId();
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setMessageId(messageId);
        Message message = new Message(JSON.toJSONString(json).getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(
                DemoRabbitExchange.DEMO_EXCHANGE
                , RabbitRoutingKey.ADD
                , message
        );
    }

}
